using System;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;

namespace Uportal.Shp.UI.Features.FeatureTJ
{
    [Guid("cb61fcdd-dbad-4a81-a374-6bb64a23743e")]
    public class FeatureTJEventReceiver : SPFeatureReceiver
    {
        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            if (properties.Feature.Parent != null)
            {
                if (properties.Feature.Parent != null)
                {
                    try
                    {
                        if (properties.Feature.Parent is SPSite)
                            CreateTJOnSite(properties);
                        else
                            if (properties.Feature.Parent is SPWeb)
                                CreateTJOnWeb(properties);
                            else
                                return;
                    }
                    catch (Exception ex)
                    {
                        //Logger.Error(ex.Message);
                    }
                }
            }
        }

        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            if (properties.Feature.Parent != null)
            {
                try
                {
                    if (properties.Feature.Parent is SPSite)
                        DeleteTJOnSite(properties);
                    else
                        if (properties.Feature.Parent is SPWeb)
                            DeleteTJOnWeb(properties);
                        else
                            return;
                }
                catch (Exception ex)
                {
                    //Logger.Error(ex.Message);
                }
            }
        }

        private void CreateTJOnWeb(SPFeatureReceiverProperties prop)
        {
            using (var web = prop.Feature.Parent as SPWeb)
            {
                if (web == null)
                    return;
                var collection = web.Site.WebApplication.JobDefinitions;
                foreach (SPJobDefinition job in collection)
                {
                    if (job.Name == "Uportal.ReminderFeedBack")
                        job.Delete();
                }
                var sfdJob = new TJ.ReminderFeedBackTJ("Uportal.ReminderFeedBack", web.Site.WebApplication);
                sfdJob.Update();
            }
        }

        private void CreateTJOnSite(SPFeatureReceiverProperties prop)
        {
            using (var site = prop.Feature.Parent as SPSite)
            {
                if (site == null)
                    return;
                var collection = site.WebApplication.JobDefinitions;
                foreach (SPJobDefinition job in collection)
                {
                    if (job.Name == "Uportal.ReminderFeedBack")
                        job.Delete();
                }
                var sfdJob = new TJ.ReminderFeedBackTJ("Uportal.ReminderFeedBack", site.WebApplication);
                sfdJob.Update();
            }
        }

        private void DeleteTJOnSite(SPFeatureReceiverProperties prop)
        {
            using (var site = prop.Feature.Parent as SPSite)
            {
                if (site == null)
                    return;
                var collection = site.WebApplication.JobDefinitions;
                foreach (SPJobDefinition job in collection)
                {
                    if (job.Name == "Uportal.ReminderFeedBack")
                        job.Delete();
                }
            }
        }

        private void DeleteTJOnWeb(SPFeatureReceiverProperties prop)
        {
            using (var web = prop.Feature.Parent as SPWeb)
            {
                if (web == null)
                    return;
                var collection = web.Site.WebApplication.JobDefinitions;
                foreach (SPJobDefinition job in collection)
                {
                    if (job.Name == "Uportal.ReminderFeedBack")
                        job.Delete();
                }
            }
        }

    }
}
